package com.dxy.lc.base;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.dxy.lc.utils.JsonUtil;
import com.fasterxml.jackson.annotation.JsonInclude;
import java.util.Collections;
import java.util.List;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Resp<T> {
    @JsonInclude
    private Boolean success;
    @JsonInclude
    private int status;
    @JsonInclude
    private String message;
    @JsonInclude
    private T data;

    public Resp() {
    }

    public static <T> Resp<T> bizSuccess(T data) {
        return (new Resp()).setSuccess(Boolean.TRUE).setStatus(StatusMessageEnum.SUCCESS.getStatus()).setMessage(StatusMessageEnum.SUCCESS.getMsg()).setData(data);
    }

    public static <T> Resp<T> bizSuccess() {
        return (new Resp()).setSuccess(Boolean.TRUE).setStatus(StatusMessageEnum.SUCCESS.getStatus()).setMessage(StatusMessageEnum.SUCCESS.getMsg());
    }

    public static Resp<List<String>> bizSuccessEmptyStringList() {
        return (new Resp()).setSuccess(Boolean.TRUE).setStatus(StatusMessageEnum.SUCCESS.getStatus()).setMessage(StatusMessageEnum.SUCCESS.getMsg()).setData(Collections.emptyList());
    }

    public static <T> Resp<T> bizFail(T data) {
        return (new Resp()).setSuccess(Boolean.FALSE).setStatus(StatusMessageEnum.FAIL.getStatus()).setMessage(StatusMessageEnum.FAIL.getMsg()).setData(data);
    }

    public static <T> Resp<T> bizFail() {
        return (new Resp()).setSuccess(Boolean.FALSE).setStatus(StatusMessageEnum.FAIL.getStatus()).setMessage(StatusMessageEnum.FAIL.getMsg());
    }

    public static <T> Resp<T> bizFail(IStatusMessage iStatusMessage) {
        return (new Resp()).setSuccess(Boolean.FALSE).setStatus(iStatusMessage.getStatus()).setMessage(iStatusMessage.getMsg());
    }

    public static <T> Resp<T> bizFail(Integer status, String message) {
        return (new Resp()).setSuccess(Boolean.FALSE).setStatus(status).setMessage(message);
    }

    public void exceptionIfUnSuccess() {
        if (!this.success) {
            throw new UnifiedException(this.status, this.message);
        }
    }

    public T dataIfSuccessOrException() {
        if (this.getSuccess()) {
            return this.getData();
        } else {
            throw new UnifiedException(this.getStatus(), this.getMessage());
        }
    }

    public T dataIfSuccessOrWarn() {
        if (!this.getSuccess()) {
            log.warn("{}", this.getMessage());
        }

        return this.getData();
    }

    public T dataIfSuccess() {
        return this.getSuccess() ? this.getData() : null;
    }

    public T notNullDataOrException() {
        this.exceptionIfUnSuccess();
        Assert.notNull(this.getData(), "data为null", new Object[0]);
        return this.getData();
    }

    public T notNullDataOrException(String message, String... args) {
        this.exceptionIfUnSuccess();
        if (this.getData() == null) {
            throw new UnifiedException(StatusMessageEnum.ABSENT, StrUtil.format(message, (Object[])args));
        } else {
            return this.getData();
        }
    }

    @Override
    public String toString() {
        return JsonUtil.toJSON(this);
    }

    public Boolean getSuccess() {
        return this.success;
    }

    public int getStatus() {
        return this.status;
    }

    public String getMessage() {
        return this.message;
    }

    public T getData() {
        return this.data;
    }

    public Resp<T> setSuccess(final Boolean success) {
        this.success = success;
        return this;
    }

    public Resp<T> setStatus(final int status) {
        this.status = status;
        return this;
    }

    public Resp<T> setMessage(final String message) {
        this.message = message;
        return this;
    }

    public Resp<T> setData(final T data) {
        this.data = data;
        return this;
    }
}

